﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN06_3V : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {   
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {            
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
            this.SetControl();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }    
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN06_1S.aspx");
    }
    #endregion
    #region "---------- Methods -----------"
    private void AddClientScript()
    {
        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");

        txtCostCenter.AddControl(hidCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.AddQueryString("bIsActive", "1");
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";


        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");

        txtGLAccountCode.AddControl(hidGLAccountCode.ClientID);
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.AddQueryString("bIsActive", "1");
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";


        btnCurrencyCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");
        txtCurrencyCode.AddControl(hidCurrencyCode.ClientID);
        txtCurrencyCode.AddControl(txtCurrencyCode.ClientID);
        txtCurrencyCode.ControlSearch = txtCurrencyCode.ClientID;
        txtCurrencyCode.AddQueryString("bIsActive", "1");
        txtCurrencyCode.SearchKey = "Currency_oCurrencyCode_oCurrencyName";       
    }
    private void BindData(int ID)
    {
        List<GSBBond> gSBBond = new List<GSBBond>();
        DataTable dt = new SPR_GSBBond().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, gSBBond);

            txtTransactionDate.Text = gSBBond[0].TransactionDate.DateToString();
            txtBondNo.Text = gSBBond[0].BondNo;
            txtCostCenter.Text = gSBBond[0].CostCenterName;
            txtGLAccountCode.Text = gSBBond[0].GLAccount;
            txtStartDate.Text = gSBBond[0].StartedDate.DateToString();
            txtEndDate.Text = gSBBond[0].EndDate.DateToString();
            ddlDayCountMethod.Text = gSBBond[0].DayCountMethodID.ToString();
            ddlPaymentDate.SelectedValue = gSBBond[0].PaymentDateID.ToString();
            txtPaymentFreq.Text = gSBBond[0].PaymentFreq.ToString();
            ddlPaymentFreqTimeBase.SelectedValue = gSBBond[0].PaymentFreqTimeBase;
            ddlPaymentCondition.SelectedValue = gSBBond[0].PaymentConditionID.ToString();
            txtCurrencyCode.Text = gSBBond[0].CurrencyCode;
            txtBondValue.Text = gSBBond[0].BondValue.ToString();
            txtAccountValue.Text = gSBBond[0].AccountValue.ToString();
            ddlEmbeddedOption.SelectedValue = gSBBond[0].EmbeddedOptionID.ToString();
            ddlPriceValueUnit.SelectedValue = gSBBond[0].PriceValueUnitID.ToString();
            ddlLicenseStatus.SelectedValue = gSBBond[0].LicenseStatusID.ToString();
            txtPriceValue.Text = gSBBond[0].PriceValue.ToString();
            txtStatus.Text = gSBBond[0].IsConfirmed == false ? "สร้างรายการ" : "ยืนยัน";                  
        }
    }
    private void BindDDL()
    {
        ddlDayCountMethod.SetData(DDL_TYPE.DayCountMethod, "", false, true);
        ddlPaymentDate.SetData(DDL_TYPE.PaymentDate, "", false, true);
        ddlPaymentFreqTimeBase.SetData(DDL_TYPE.PaymentFrequencyTimeBase, "", false, true);
        ddlPaymentCondition.SetData(DDL_TYPE.PaymentCondition, "", false, true);
        ddlEmbeddedOption.SetData(DDL_TYPE.EmbeddedOption, "", false, true);
        ddlPriceValueUnit.SetData(DDL_TYPE.PriceValueUnit, "", false, true);
        ddlLicenseStatus.SetData(DDL_TYPE.LicenseStatus, "", false, true);
    }
    private void SetControl()
    {
        SetControlReadOnly(form_control);
    }
    protected void SetControlReadOnly(PlaceHolder plcReadOnly)
    {
        foreach (Control ctrl in plcReadOnly.Controls)
        {
            if (ctrl is Button)
            {
                ((Button)ctrl).Enabled = false;
            }
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Attributes["readOnly"] = "true";
                ((TextBox)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is DropDownList)
            {
                ((DropDownList)ctrl).Enabled = false;
                ((DropDownList)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is RadioButton)
            {
                ((RadioButton)ctrl).Attributes["readOnly"] = "true";
                ((RadioButton)ctrl).BackColor = System.Drawing.Color.FromArgb(224, 224, 224);
            }
            else if (ctrl is Image)
            {
                ((Image)ctrl).Attributes["disabled"] = "true";
            }
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN06_3V;
    }
    #endregion
}